package com.smalls.redshoes.ui.widget;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.smalls.newvideotwo.mvp.bean.LiveChannel;
import com.smalls.newvideotwo.service.FloatService;
import com.smalls.newvideotwo.util.LogUtil;
import com.smalls.redshoes.R;
import com.smalls.redshoes.ui.fragment.LivePlayFragment;
import com.smalls.redshoes.ui.widget.LivePlayControlProgramsView.OnLiveProgramItemListener;

import java.util.ArrayList;

public class LivePlayControlView extends RelativeLayout {
    public static final String DEFAULT_NETSPEED = "0KB/S";
    public static final int SET_NETWORK_SPEED_INTERVAL = 2000;
    private static String TAG = com.smalls.newvideotwo.utils.Constant.baddcoolio2tag? com.smalls.newvideotwo.utils.Constant.TAGPREFIXSTR + LivePlayControlView.class.getSimpleName():LivePlayControlView.class.getSimpleName();
    private TextView mCenterStateTv;
    private LivePlayControlProgramsView mControlProgramsView;
    private ArrayList<LiveChannel> mDatas;
    private String mJniErrorStr;
    private long mLastSetTime;
    private View mRootView;
    private int mState;
    private BroadcastReceiver netSpeedReceiver;
    private OnLiveProgramItemListener onLiveProgramItemListener;

    public void setOnLiveProgramItemListener(OnLiveProgramItemListener onLiveProgramItemListener) {
        this.onLiveProgramItemListener = onLiveProgramItemListener;
        if (this.mControlProgramsView != null) {
            this.mControlProgramsView.setOnLiveProgramItemListener(this.onLiveProgramItemListener);
        }
    }

    public void changeProgram(int position) {
        LogUtil.d(TAG, "changeProgram=" + position);
        if (this.mControlProgramsView != null) {
            this.mControlProgramsView.changeProgram(position);
        }
    }

    public boolean setVisible(int keycode) {
        if (this.mControlProgramsView == null) {
            return false;
        }
        if (this.mControlProgramsView.getVisibility() != View.VISIBLE) {
            this.mControlProgramsView.setVisibility(View.VISIBLE);
            return true;
        }
        this.mControlProgramsView.onKeyDown(keycode);
        return false;
    }

    public void setState(int mState) {
        this.mState = mState;
        switch (this.mState) {
            case -3 /*-3*/:
                this.mControlProgramsView.stopLoadingAnimation();
                if (this.mCenterStateTv.getVisibility() == View.VISIBLE) {
                    this.mCenterStateTv.clearAnimation();
                }
                this.mControlProgramsView.setAutoHide(false);
                return;
            case -2 /*-2*/:
                this.mControlProgramsView.stopLoadingAnimation();
                if (this.mCenterStateTv.getVisibility() != View.VISIBLE) {
                    this.mCenterStateTv.setVisibility(View.VISIBLE);
                }
                this.mControlProgramsView.setAutoHide(false);
                setStateText(getResources().getString(R.string.loading_bad_error));
                return;
            case -1 /*-1*/:
                this.mControlProgramsView.stopLoadingAnimation();
                if (this.mCenterStateTv.getVisibility() != View.VISIBLE) {
                    this.mCenterStateTv.setVisibility(View.VISIBLE);
                }
                this.mControlProgramsView.setAutoHide(false);
                setStateText(getResources().getString(R.string.loading_error));
                return;
            case LivePlayFragment.DEFAULT_PROGRAM_INDEX /*0*/:
                this.mControlProgramsView.startLoadingAnimation();
                if (this.mCenterStateTv.getVisibility() != View.VISIBLE) {
                    this.mCenterStateTv.setVisibility(View.VISIBLE);
                }
                this.mControlProgramsView.setAutoHide(false);
                setStateText(getResources().getString(R.string.loading_programs));
                return;
            case 1 /*1*/:
                this.mControlProgramsView.startLoadingAnimation();
                if (this.mCenterStateTv.getVisibility() != View.VISIBLE) {
                    this.mCenterStateTv.setVisibility(View.VISIBLE);
                }
                this.mControlProgramsView.setAutoHide(true);
                setStateText(getResources().getString(R.string.buffering));
                return;
            case 2 /*2*/:
                LogUtil.d(TAG, "STATE_PLAYING");
                this.mControlProgramsView.stopLoadingAnimation();
                this.mControlProgramsView.setSateTextNull();
                if (this.mCenterStateTv.getVisibility() == View.VISIBLE) {
                    this.mCenterStateTv.setVisibility(GONE);
                }
                this.mControlProgramsView.setAutoHide(true);
                return;
            default:
                return;
        }
    }

    public int getState() {
        return this.mState;
    }

    private void setStateText(String text) {
        if (!TextUtils.isEmpty(text)) {
            if (this.mControlProgramsView != null) {
                this.mControlProgramsView.setStateText(text);
            }
            if (this.mCenterStateTv != null) {
                this.mCenterStateTv.setText(text);
            }
        }
    }

    public void setmJniErrorStr(String mJniErrorStr) {
        this.mJniErrorStr = mJniErrorStr;
        if (this.mState == -3 && !TextUtils.isEmpty(this.mJniErrorStr)) {
            setStateText(this.mJniErrorStr);
        }
    }

    public LivePlayControlView(Context context) {
        this(context, null);
    }

    public LivePlayControlView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LivePlayControlView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mState = 0;
        this.mLastSetTime = 0;
        this.netSpeedReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals(FloatService.ActionNetSpeedChanged)) {
                    String text = intent.getStringExtra(FloatService.KeyNetSpeedChanged);
                    if (!TextUtils.isEmpty(text)) {
                        if (text.contains("-")) {
                            text = LivePlayControlView.DEFAULT_NETSPEED;
                        }
                        if (LivePlayControlView.this.mControlProgramsView != null
                                && SystemClock.uptimeMillis() - LivePlayControlView.this.mLastSetTime >= 2000L) {
                            LivePlayControlView.this.mControlProgramsView.setNetSpeed(text);
                            LivePlayControlView.this.mLastSetTime = SystemClock.uptimeMillis();
                        }
                    }
                }
            }
        };
        init(context);
    }

    private void init(Context context) {
        this.mRootView = LayoutInflater.from(context).inflate(R.layout.rs_layout_live_play_control, this, true);
        this.mControlProgramsView = (LivePlayControlProgramsView) this.mRootView.findViewById(R.id.control_view);
        this.mCenterStateTv = (TextView) this.mRootView.findViewById(R.id.tv_notice);
        registerNetSpeedReceiver();
    }

    public void updateVisibity() {
        if (this.mControlProgramsView != null) {
            this.mControlProgramsView.setVisibility(this.mControlProgramsView.getVisibility() == View.VISIBLE ? GONE : VISIBLE);
        }
    }

    public void updateProgramListVisibility() {
    }

    public void updateInfoBarVisibility() {
    }

    public void setTime() {
        if (this.mControlProgramsView != null) {
            this.mControlProgramsView.setTime();
        }
    }

    public void setmDatas(ArrayList<LiveChannel> mDatas) {
        this.mDatas = mDatas;
        if (this.mControlProgramsView != null) {
            this.mControlProgramsView.setDatas(this.mDatas);
        }
    }

    public void notifyDataSetChanged() {
        if (this.mControlProgramsView != null) {
            this.mControlProgramsView.notifyDataSetChanged();
        }
    }

    public void destory() {
        unregisterNetSpeedReceiver();
        if (this.mControlProgramsView != null) {
            this.mControlProgramsView.destory();
        }
    }

    private void registerNetSpeedReceiver() {
        getContext().getApplicationContext().registerReceiver(this.netSpeedReceiver, new IntentFilter(FloatService.ActionNetSpeedChanged));
    }

    public int isVisibility() {
        return this.mControlProgramsView == null ? 8 : this.mControlProgramsView.getVisibility();
    }

    public void setGone() {
        if (this.mControlProgramsView != null) {
            this.mControlProgramsView.setVisibility(GONE);
        }
    }

    private void unregisterNetSpeedReceiver() {
        try {
            getContext().unregisterReceiver(this.netSpeedReceiver);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
    }
}
